package com.example.myanimator.shadowLayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/30
 * @describe BitmapShader 的基本使用
 */
public class BitmapShadowView extends View {

    private Paint mPaint;
    private Bitmap mBitmap;
    private BitmapShader mBitmapShader;

    public BitmapShadowView(Context context) {
        super(context);
    }

    public BitmapShadowView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BitmapShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);

        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setDither(true); // 抗抖动
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(100);//设置文字大小
        mPaint.setColor(Color.BLACK);

        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.MIRROR,Shader.TileMode.MIRROR);

        mPaint.setShader(mBitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText("BitmapShader的基本用法",0,mPaint.getFontMetrics().bottom-mPaint.getFontMetrics().top,mPaint);
        float left = getWidth () /3 ;
        float top = getHeight () /3;
        float right = getWidth () *2/3 ;
        float bottom = getHeight () *2/3 ;
//        canvas.drawRect(0,mPaint.getFontMetrics().bottom-mPaint.getFontMetrics().top,getWidth(),getHeight(),mPaint);
        canvas.drawRect(left,top,right,bottom,mPaint);
    }



}
